<?php
namespace Admin\Controller;
use Think\Controller;


class UserController extends Controller {

    public function useradd(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        if (!IS_POST) {
            $this->display();
        }else{
            //接收文件 调用文件上传
            $upload = new \Admin\Controller\MyUploadController();
            $info = $upload->upload();
            if (!$info) {
                $this->ajaxReturn(ajax_data(1,'上传失败'));
            } else {
                $_POST['user_img'] = $info['pic'];
            }
            $artModel=D('user');
            if ($artModel->add($_POST)) {
                $this->ajaxReturn(ajax_data(0,'成员添加成功'));
            }else{
                $this->ajaxReturn(ajax_data(1,'成员添加失败'));
            }
        }
    }
    public function userlist(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        $artModel = D('user');

        $count = $artModel->where(array('is_del' => '0'))->count();
        $Page  = new \Think\Page($count,10);
        $arts = $artModel->where('is_del=0')->order('sort desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();
        $this->assign('page',$show);
        $this->assign('arts',$arts);
        $this->display();
    }

    public function useredit(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        $artModel = D('user');
        if (!IS_POST) {
            $art_id = I('aid');
            $art  = $artModel->find($art_id);
            $this->assign('art',$art); 
            $this->display();
        }else{
            if ($_FILES['pic']['error'] == 0) {
                //接收文件 调用文件上传
                $upload = new \Admin\Controller\MyUploadController();
                $info = $upload->upload();
                if (!$info) {
                    $this->ajaxReturn(ajax_data(1,'上传失败'));
                } else {
                    $_POST['user_img'] = $info['pic'];
                }
            }

            //存储数据
            $bannerModel=M('user');
            $data = $bannerModel->create();

            if($bannerModel->where('uid='.I('get.uid'))->save()){
                $this->ajaxReturn(ajax_data(0,'修改成功'));
            }else{
                $this->ajaxReturn(ajax_data(1,'修改失败'));
            }
        }
    }
    public function del(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        $aid = I('get.id');
        $res = M('article')->delete($aid);
        if ($res) {
            $this->success('删除成功',(U('Admin/art/artlist')));
        }else{
            $this->error('删除失败');
        }
    }
    public function ueditor(){
        $data = new \Org\Util\Ueditor();
        echo $data->output();
    }
    public function sort(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        //存储数据
        $user=M('user');
        $data['sort'] = $_POST['sort'];
        if($user->where('uid='.$_POST['uid'])->save($data)){
            $this->ajaxReturn(ajax_data(0,'修改成功'));
        }else{
            $this->ajaxReturn(ajax_data(1,'修改失败'));
        }
    }
    public function systemedit()
    {
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        $artModel = D('system');
        if (!IS_POST) {
            $art = $artModel->find(1);
            $this->assign('art', $art);
            $this->display();
        } else {
            $data['content'] = $_POST['content'];
            if ($artModel->where('id=1')->save($data)) {
                $this->success("修改成功", (U('Admin/user/systemedit')));
            } else {
                $this->error("修改失败");
            }
        }
    }
}